<?php
/**
 * Created by PhpStorm.
 * User: liu
 * Date: 17-4-15
 * Time: 下午11:00
 */

namespace Cframe;


class AutoCreate {
    public function __construct(){
        if (is_dir(CON_PATH . BIND_MODULE . DS . APP_NAME . DS)){
            return true;
        }
        $flodList = [
            "controller"=> CON_PATH . BIND_MODULE . DS . APP_NAME . DS,
            "model" => CON_PATH . BIND_MODULE . DS . BIND_MODEL . DS,
            'view'=> CON_PATH . BIND_MODULE . DS . BIND_VIEW . DS,
        ];
        $content =<<<PP
<?php 
namespace :con\:modu\:app;
use Cframe\Controller;

class :cotro extends Controller {
    public function :action(){
        return '<h1>欢迎使用Cframe</h1>';
    }
}
PP;
        $content = str_replace([":con",":modu",":app",":cotro",":action"],[CON_NAME,BIND_MODULE,APP_NAME,BIND_CONTROLLER,BIND_ACTION],$content);
        $fileList = [
            $flodList['controller'] . BIND_CONTROLLER . ".php" => $content,
            CON_PATH . "Config.php" => "<?php \n\n // 此处填写配置",
            CON_PATH . "Database.php" => "<?php \n\n // 此处填写数据库配置",
            CON_PATH . "helper.php" => "<?php \n\n // 此处填写项目帮助函数",
            CON_PATH . "route.php" => "<?php \n\n // 此处填写路由",
        ];
        foreach ($flodList as $k){
            if (!is_dir($k)){
                @mkdir($k,0777,true);
            }
        }
        foreach ($fileList as $k=>$value){
            if (!file_exists($k)){
                file_put_contents($k,$value);
            }
        }
        return true;
    }
}